<?php
namespace app\admin\controller;

use app\admin\model\Channel as ChannelModel;
use app\admin\model\Platfrom as PlatfromModel;
use app\admin\model\ReviewPhone as ReviewPhoneModel;
use app\admin\model\SmsReview as SmsReviewModel;

/**
 * 短信审核
 * @Author    Eyong
 * @DateTime  2018-06-20T09:40:52+0800
 */
class SmsReview extends Father {
	/**
	 * [index 审核首页]
	 * @Author   Eyong
	 * @DateTime 2018-06-20T09:40:52+0800
	 * @return   [type]                   [description]
	 */
	public function index() {

		$request = dataProcess(request()->only(['cid' => '', 'st' => date('Y-m-d'), 'api' => '', 'phone_type' => '', 'page' => '', 'limit' => '', 'create_time' => '00:00:00', 'end_time' => date('H:i:s'), 'phone' => '', 'isp_host' => '', 'platId' => '', 'send_text' => '']));
		if (input('get.api')) {
			$request['table_time']  = substr(date('Ymd', strtotime($request['st'])), 2);
			$request['create_time'] = $request['st'] . ' ' . $request['create_time'];
			$request['end_time']    = $request['st'] . ' ' . $request['end_time'];

			$data  = SmsReviewModel::getListAll($request);
			$count = $data['total'];

			$list = $data['data'];

			return json(['code' => 0, 'msg' => '', 'count' => $count, 'data' => $list]);

		} else {
			if (is_OneCha()) {

				$chas = ChannelModel::field('channel_id,company_name')->where('p_id', session('relate_id'))->whereor('channel_id', session('relate_id'))->select();
			} else {

				$chas = ChannelModel::field('channel_id,company_name')->select();
			}
			$plats = PlatfromModel::field('plat_id,platfrom')->select();
			$this->assign('chas', $chas);
			$this->assign('plats', $plats);
			$this->assign('requests', $request);
			return $this->fetch();
		}
	}

	/**
	 * 审核短信
	 * @Author   Eyong
	 * @DateTime 2018-06-26T14:33:57+0800
	 * @return   [type]                   [description]
	 */
	public function review() {
		if (request()->isPost()) {

			$request       = dataProcess(request()->only(['status', 'st', 'reviewId', 'desc', 'cid', 'phone_type']));
			$request['st'] = substr(date('Ymd', strtotime($request['st'])), 2);
			return json(SmsReviewModel::reviewSms($request));
		} else {
			return json(['code' => 200, 'msg' => '非法请求']);
		}
	}
	/**
	 * [batchreview 批量审核]
	 * @return [type] [description]
	 */
	public function batchreview() {
		if (request()->isPost()) {

			$request                = dataProcess(request()->only(['status', 'st', 'reviewId', 'desc', 'cid', 'phone_type', 'number', 'create_time', 'end_time', 'isp', 'phone', 'platId', 'channel_order_id', 'send_text']));
			$request['create_time'] = $request['st'] . ' ' . $request['create_time'];
			$request['end_time']    = $request['st'] . ' ' . $request['end_time'];
			$request['st']          = substr(date('Ymd', strtotime($request['st'])), 2);
			return json(SmsReviewModel::batchReviewSms($request));
		} else {
			return json(['code' => 200, 'msg' => '非法请求']);
		}
	}

	/**
	 * [smsPhone 审核短信对应号码]
	 * @return [type] [description]
	 */
	public function smsphone() {

		if (input('get.api')) {
			$request = dataProcess(request()->only(['tableId', 'st', 'api', 'page', 'limit', 'phone']));

			$data = ReviewPhoneModel::getListAll($request);
			if ($data) {
				$count = $data['total'];
				$list  = $data['data'];
			} else {
				$count = 0;
				$list  = [];
			}

			return json(['code' => 0, 'msg' => '', 'count' => $count, 'data' => $list]);

		} else {
			$request = request()->param();
			$this->assign('request', $request);
			return $this->fetch();
		}
	}

	/**
	 * [reviewlist 所有审核的列表]
	 * @return [type] [description]
	 */
	public function reviewlist() {
		$request = dataProcess(request()->only(['cid', 'st' => date('Ymd'), 'api', 'page', 'limit']));
		if (input('get.api')) {
			// halt($request);
			$request['st'] = substr(date('Ymd', strtotime($request['st'])), 2);

			$data = SmsReviewModel::reviewChannels($request);
			// halt($data);
			$count = $data['total'];

			$list = $data['data'];

			return json(['code' => 0, 'msg' => '', 'count' => $count, 'data' => $list]);

		} else {
			$this->assign('requests', $request);
			return $this->fetch();
		}
	}

	/**
	 * [reviewSms 单条审核]
	 * @return [type] [description]
	 */
	public function reviewsms() {
		if (request()->isPost()) {
			$request               = dataProcess(request()->only(['status', 'st', 'reviewId', 'desc']));
			$request['table_time'] = substr(date('Ymd', strtotime($request['st'])), 2);
			return json(SmsReviewModel::reviewOne($request));
		} else {
			// halt(input('get.'));
			$this->assign('reviewId', trim(input('get.id')));
			$this->assign('st', trim(input('get.st')));
			return $this->fetch('review');
		}
	}

	/**
	 * [approved 审核通过页面]
	 * @return [type] [description]
	 */
	public function approved() {
		$request = dataProcess(request()->only(['cid' => '', 'state' => '', 'st' => date('Y-m-d'), 'api' => '', 'phone_type' => '', 'page' => '', 'limit' => '', 'create_time' => '00:00:00', 'end_time' => date('H:i:s'), 'phone' => '', 'isp_host' => '', 'platId' => '', 'send_text' => '']));
		// halt($request);
		if (input('get.api')) {
			$request['table_time']  = substr(date('Ymd', strtotime($request['st'])), 2);
			$request['create_time'] = $request['st'] . ' ' . $request['create_time'];
			$request['end_time']    = $request['st'] . ' ' . $request['end_time'];
			// halt($request);

			$data = SmsReviewModel::getAllSms($request);
			// halt($data);
			$count = $data['total'];

			$list = $data['data'];

			return json(['code' => 0, 'msg' => '', 'count' => $count, 'data' => $list]);

		} else {
			// halt($request);
			if (is_OneCha()) {

				$chas = ChannelModel::field('channel_id,company_name')->where('p_id', session('relate_id'))->whereor('channel_id', session('relate_id'))->select();
			} else {

				$chas = ChannelModel::field('channel_id,company_name')->select();
			}
			$plats = PlatfromModel::field('plat_id,platfrom')->select();
			$this->assign('chas', $chas);
			$this->assign('plats', $plats);
			$this->assign('requests', $request);
			return $this->fetch();
		}
	}
}
